package com.att.action;

import com.att.bean.NumRunDeilBean;
import com.att.bean.UserOfRunBean;
import com.att.bean.UserOfRunDeilBean;
import com.att.bean.UserStatisticsBean;
import com.att.service.NumRunDeilService;
import com.att.service.UserOfRunDeilService;
import com.att.service.UserOfRunService;
import com.att.service.UserStatisticsService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("userStatistics")
public class UserStatisticsAction {

    @Resource(name = "userStatisticsService")
    private UserStatisticsService userStatisticsService;
    @Resource(name = "userOfRunService")
    private UserOfRunService userOfRunService;
    @Resource(name = "userOfRunDeilService")
    private UserOfRunDeilService userOfRunDeilService;
    @Resource(name = "numRunDeilService")
    private NumRunDeilService numRunDeilService;

    //新增
    @RequestMapping("insert")
    @ResponseBody
    @Transactional
    public String insert(UserStatisticsBean bean) throws ParseException {
        UserOfRunBean userOfRunBean = new UserOfRunBean();
        String[] userId = bean.getUserId().split(",");
        for (int i = 0; i < userId.length; i++) {
            userOfRunBean.setUserId(userId[i]);
            List<UserOfRunBean> list = userOfRunService.findUserOfRunBean(userOfRunBean);
            userOfRunBean.setNumRunId(bean.getNumRunId());
            userOfRunBean.setStartTime(bean.getStartTime());
            userOfRunBean.setEndTime(bean.getEndTime());
            userOfRunBean.setRunSort(list.size() + 1);
            boolean flag = userOfRunService.insert(userOfRunBean);
            if (flag) {
                UserOfRunDeilBean userOfRunDeilBean = new UserOfRunDeilBean();
                userOfRunDeilBean.setStartTimeStr(bean.getStartTimeStr());
                userOfRunDeilBean.setEndTimeStr(bean.getEndTimeStr());
                userOfRunDeilBean.setNumRunId(bean.getNumRunId());
                userOfRunDeilBean.setUserId(userId[i]);
                userOfRunDeilBean.setDeilType(bean.getDeilType());
                userOfRunDeilBean.setSchClassId(bean.getSchClassId());
                userOfRunDeilBean.setAddWorkTime(bean.getAddWorkTime());
                userOfRunDeilBean.setIsAddWork(bean.getIsAddWork());
                NumRunDeilBean numRunDeilBean = new NumRunDeilBean();
                numRunDeilBean.setNumRunId(bean.getNumRunId());
                List<NumRunDeilBean> beanList = numRunDeilService.findNumRunDeilById(numRunDeilBean);
                for (int k = 0;k<beanList.size();k++){
                    userOfRunDeilBean.setSchClassId(beanList.get(k).getSchClassId());
                    boolean fa = userOfRunDeilService.insert(userOfRunDeilBean);
                    if (!fa) {
                        return "ERROR";
                    }
                }

                List<UserStatisticsBean> list1 = userStatisticsService.findUserStatisticsById(userId[i]);
                if (list1.size() <= 0) {
                    bean.setUserId(userId[i]);
                    boolean flag2 = userStatisticsService.insert(bean);
                    if (!flag2) {
                        return "ERROR";
                    }
                }else {
                    bean.setUserId(userId[i]);
                    boolean flag2 = userStatisticsService.update(bean);
                    if (!flag2) {
                        return "ERROR";
                    }
                }
            }
        }
        return "SUCCESS";
    }


    /**
     * Description: 查询人员排班信息
     * @param userId
     */
    @RequestMapping("findUserStatisticsById")
    @ResponseBody
    public Object findUserStatisticsBean(String userId) {
        List<UserStatisticsBean> list = userStatisticsService.findUserStatisticsById(userId);
        return  list;
    }
}
